package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.ProductDao;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Order;
import cn.sumpu.app.guoguojie.domain.OrderProduct;
import cn.sumpu.app.guoguojie.domain.Product;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class ProductDaoImpl extends SqlSessionDaoSupport implements ProductDao{
	
	//增加产品
	@Override
	public void insertNewProduct(Product product) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.ProductMapper.insertProduct",
					product);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertNewProduct rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertNewProduct rushdatabase failure.", e);
		}
		
	}
	//添加订单
	@Override
	public void insertOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.ProductMapper.insertOrder",
					order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertOrder rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertOrder rushdatabase failure.", e);
		}
		
	}
	//添加产品 订单关联表
	@Override
	public void insertOrderProduct(OrderProduct orderProduct) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.ProductMapper.insertOrderProduct",
					orderProduct);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertOrderProduct rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertOrderProduct rushdatabase failure.", e);
		}
		
	}
	
	
	//查询产品
	@SuppressWarnings("unchecked")
	@Override
	public List<Product> selectProductList(Map<String, Object> map)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectProductList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectProductList database failure.", e);
			}
			throw new AppBusinessException("selectProductList database failure.", e);
		}
	}
	//查询兑奖达人
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getOrderMemberList(Map<String, Object> map)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.getOrderMemberList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderMemberList database failure.", e);
			}
			throw new AppBusinessException("getOrderMemberList database failure.", e);
		}
	}
	
	// 查询产品总数
	@Override
	public int countProductList(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.ProductMapper.countProductList",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("countProductList database failure.", e);
			}
			throw new AppBusinessException(
					"countProductList database failure.", e);
		}
	}
	//删除产品
	@Override
	public void deleteProductById(String identifier)throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ProductMapper.deleteProductById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteProductById database failure.", e);
			}
			throw new AppBusinessException("deleteProductById database failure.", e);
		}
	}
	//删除订单
	@Override
	public void deleteOrderById(String identifier)throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ProductMapper.deleteOrderById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteOrderById database failure.", e);
			}
			throw new AppBusinessException("deleteOrderById database failure.", e);
		}
	}
	//删除订单与产品关联表
	@Override
	public void deleteOrderProductById(String orderId)throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ProductMapper.deleteOrderProductById",
							orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteOrderProductById database failure.", e);
			}
			throw new AppBusinessException("deleteOrderProductById database failure.", e);
		}
	}
	//查询某一订单
	
	@Override
	public Order selectViewOrder(String orderId)throws AppBusinessException{
		try {
			return (Order)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectViewOrder",
					orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectViewOrder database failure.", e);
			}
			throw new AppBusinessException("selectViewOrder database failure.", e);
		}
	}
	
	
	//查询某一产品
	
	@Override
	public Product selectProduct(String identifier)throws AppBusinessException{
		try {
			return (Product)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectProduct",
					identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectProduct database failure.", e);
			}
			throw new AppBusinessException("selectProduct database failure.", e);
		}
	}
	//查询今日推荐
	@Override
	public Product selectRecommendProduct()throws AppBusinessException{
		try {
			return (Product)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectRecommendProduct"
					);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRecommendProduct database failure.", e);
			}
			throw new AppBusinessException("selectRecommendProduct database failure.", e);
		}
	}
	
	
	
	//通过名字查询产篇
	@Override
	public Product selectProductByName(String productName)throws AppBusinessException{
		try {
			return (Product)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectProductByName",
					productName);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectProduct database failure.", e);
			}
			throw new AppBusinessException("selectProduct database failure.", e);
		}
	}
	//产询订单内产品详情
	@Override
	public OrderProduct selectOrderProductByOrderId(String orderId)throws AppBusinessException{
		try {
			return (OrderProduct)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectOrderProductByOrderId",
					orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectProduct database failure.", e);
			}
			throw new AppBusinessException("selectProduct database failure.", e);
		}
	}
	
	
	//更新商品
	@Override
	public void updateProductById(Product product)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ProductMapper.updateProductById",
							product);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateProductById database failure.", e);
			}
			throw new AppBusinessException("updateProductById database failure.", e);
		}
	}
	//今天推荐置空
	@Override
	public void updateIsClosed()throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ProductMapper.updateIsClosed"
							);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateIsClosed database failure.", e);
			}
			throw new AppBusinessException("updateIsClosed database failure.", e);
		}
	}
	
	//查询所有订单
	@SuppressWarnings("unchecked")
	@Override
	public List<Order> selectOrderList(Map<String, Object> map)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectOrderList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectProductList database failure.", e);
			}
			throw new AppBusinessException("selectProductList database failure.", e);
		}
	}
	//产询订单总数
	@Override
	public int selectOrderListCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.ProductMapper.selectOrderListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("countProductList database failure.", e);
			}
			throw new AppBusinessException(
					"countProductList database failure.", e);
		}
	}
	//更新订单
	@Override
	public void updateOrder(Order order)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ProductMapper.updateOrder",
							order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateOrder database failure.", e);
			}
			throw new AppBusinessException("updateOrder database failure.", e);
		}
	}
	//更新订单和产品表
	@Override
	public void updateOrderProduct(OrderProduct orderProduct)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ProductMapper.updateOrderProduct",
							orderProduct);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateOrderProduct database failure.", e);
			}
			throw new AppBusinessException("updateOrderProduct database failure.", e);
		}
	}
	
}
